<?php

class GuestbookAction extends Action 
{
	public function _initialize()
	{
		$this->assign('jumpUrl',__URL__);
 		if(!isset($_SESSION['adminid']))
 		{ 
 			 $url=__APP__.'/Index/login';
 			 redirect($url);return;
 		}		
	}  
	public function index()
	{
		$Guestbook=D("Guestbook");
		$count=$Guestbook->count();
		import("ORG.Util.Page");
		$listRows=15;
		$p=new page($count,$listRows);
		$list=$Guestbook->findAll(null,'*','addtime desc',$p->firstRow.','.$p->listRows);
		$page=$p->show();
		if ($list!==false) 
		{
			$this->assign('page',$page);
			$this->assign('list',$list);
		}
		$this->display();		
	}
	public function view()
	{
		$id=isset($_GET['id'])?(int)($_GET['id']):0;
		$temp=D("Guestbook")->find($id);
		if($id==0 || !$temp)
		{
			$this->redirect();return;
		}
		$this->assign('vo',$temp);
		$this->display();
	}
	public function reply()
	{
		if(!$this->isPost())
		{
			$id=isset($_GET['id'])?(int)($_GET['id']):0;
			$temp=D("Guestbook")->find($id);
			if($id==0 || !$temp)
			{
				$this->redirect();return;
			}
			$this->assign('vo',$temp);			
			$this->display();
		}
		else
		{
			$id=isset($_POST['id'])?(int)($_POST['id']):0;
			$reply=isset($_POST['reply'])?trim($_POST['reply']):'';
			if($id==0)
			{
				$this->redirect();return;
			}
			if($reply=='')
			{
				$msg="回复内容不能为空";
				$this->error($msg);return;
			}
			$data=array('id'=>$id,'reply'=>$reply,'replytime'=>time(),'replyer'=>$_SESSION['username']);
			$Guestbook=D("Guestbook");
			if(!$Guestbook->create($data))
			{
				$this->error($Guestbook->getError());return;
			}
			if(!$Guestbook->save())
			{
				$msg="回复留言出错";
				$this->error($msg);return;
			}
			$msg="回复留言成功";
			$this->assign('jumpUrl',__URL__);
			$this->success($msg);
		}
	}
	public function del()
	{
		$id=isset($_GET['id'])?(int)($_GET['id']):0;
		if (!$id){ $this->error(L('_SELECT_NOT_EXIST_'));return;}
		$Guestbook=D("Guestbook");
		$list=$Guestbook->find($id);
		if (!$list){ $this->error(L('_SELECT_NOT_EXIST_'));return;}
		if(!$Guestbook->deleteById($id)){
			$this->error(L('_DELETE_FAIL_'));return;
		}
		$this->redirect();		
	}
}
?>